//判断页面ajax请求加载成功的次数
var globalCount = 0;
$("#fakeloader").fakeLoader({
	timeToHide : 10000, //Time in milliseconds for fakeLoader disappear
	zIndex : 999, // Default zIndex
	spinner : "spinner6", //Options: 'spinner1', 'spinner2', 'spinner3', 'spinner4', 'spinner5', 'spinner6', 'spinner7' 
	bgColor : "#fff", //Hex, RGB or RGBA colors
});
setTimeout(function() {
	$('body').css('opacity', '1');
	$('body').attr("class", "gray-bg") //添加样式
}, 100);

$(document).ready(function() {

	initResourceCount();
	selectResource();

});


var returnAllCount = function() {
	if (globalCount == 2) {
		setTimeout(function() {
			$('#fakeloader').css('display', 'none');
		}, 500);
	}
}

var initResourceCount = function() {
	$.ajax({
		url : '/headline/getheadlinelistbypage',
		type : 'get',
		data : {
			pageIndex : 1,
			pageSize : 999,
			status : 0,
			typeNum : 0
		},
		dataType : 'json',
		success : function(data) {
			if(data.code===402){
				window.location.href='/login.html';
			}
			var count = 0;
			var count1 = 0;
			$(".a-2").html('0 条');
			$(".a-3").html('0 条');
			for (var i = 0; i < data.headLineList.length; i++) {
				if (data.headLineList[i].status == 1) {
					count++;
					$(".a-2").html(count + ' 条');
				} else if (data.headLineList[i].status == -1) {
					count1++;
					$(".a-3").html(count1 + ' 条');
				}
			}
			$(".a-1").html((count + count1) + ' 条');
		},
		error : function() {
			swal("初始化资源数目错误", "请重新操作", "error");
		}
	});

	globalCount++;
	returnAllCount();
}

//草稿/发布...按钮绑定查询事件  
$(".share").click(function() {
	var statu = $(this).val();
	var params = $('#allResource').bootstrapTable('getOptions');
	params.queryParams = function(params) {
		return {
			pageSize : params.limit,
			pageIndex : (params.offset) / params.limit + 1,
			title : $(".search .form-control").val(),
			status : statu,
		}
	};
	$('#allResource').bootstrapTable('refresh', params);
});


//初始化表格数据
var selectResource = function() {
	$('#allResource').bootstrapTable({
		method : 'post',
		url : "/headline/getheadlinelistbypage",
		dataType : "json",
		striped : false, //使表格带有条纹  
		pagination : true, //在表格底部显示分页工具栏  
		pageSize : 10,
		pageNumber : 1,
		sortOrder : "asc",
		sortable : true,
		pageList : [ 10, 20, 30 ],
		idField : "headLineId", //标识哪个字段为id主键  
		showToggle : false, //名片格式  
		cardView : false, //设置为True时显示名片（card）布局  
		showColumns : true, //显示隐藏列    
		showRefresh : true, //显示刷新按钮  
		search : true, //是否显示搜索框
		searchOnEnterKey : true, //设置为 true时，按回车触发搜索方法，否则自动触发搜索方法
		queryParams : queryParams, //参数  
		sidePagination : "server", //服务端处理分页
		searchTimeOut : 500, //设置搜索超时时间
		toolbarAlign : 'left', //工具栏对齐方式
		buttonsAlign : 'right', //按钮对齐方式
		toolbar : '#toolbar', //指定工作栏
		searchAlign : 'right',
		contentType : "application/x-www-form-urlencoded",
		formatLoadingMessage : function() {
			return "请稍等，正在加载中...";
		},
		formatNoMatches : function() { //没有匹配的结果  
			return "无符合条件的记录";
		},
		responseHandler : function(res) {
			return {
				"total" : res.count, //总页数
				"rows" : res.headLineList //数据
			};
		},
		
		//显示图片
		/*detailView : true,
		detailFormatter : function(index, row) {
			var image = '<div class="photos">'
						+ '<img alt="image" class="feed-photo" src="' + row['headLineImage'] + '">'
						+ '</div>';
			return image;
		},*/
		
		columns : [
			{
				title : '序号',
				align : 'center',
				valign : 'middle',
				width : '5%',
				formatter : function(value, row, index) {
					var index1 = index + 1;
					var id = '<span title="ID:' + row.headLineId + '">' + index1 + '</span>';
					return id;
				}
			},
			
			{
                field: 'headLineImage',//域值
                title: '封面',//标题
                visible: true,//false表示不显示
                sortable: true,//启用排序
                width : '10%',
                formatter : function (value, row, index) {
                    return "<img style='width: 50px;height: 50px;' src='" + value + "' alt=''>"
                }
            },

			{
				title : '名称',
				field : 'headLineTitle',
				align : 'center',
				width : '10%',
				cellStyle : formatTableUnit,
				formatter : operateOpinionFormatter
			},
			{
				title : '链接',
				field : 'headLineLink',
				align : 'center',
				width : '15%',
				cellStyle : formatTableUnit,
				formatter : operateOpinionFormatter
			},
			{
				title : '权重',
				field : 'priority',
				align : 'center',
				width : '5%',
				
			},
			{
				title : '类型',
				field : 'typeNum',
				align : 'center',
				width : '5%',
				formatter : function(value, row, index) {
					if (row.typeNum == 1) {
						return '<button class="btn-xs btn-warning">左上</button> ';
					} else if (row.typeNum == 2) {
						return '<button class="btn-xs btn-info" >右上</button> ';
					}

				}
			},

			{
				title : '状态',
				field : 'status',
				align : 'center',
				width : '5%',
				formatter : function(value, row, index) {
					if (row.status == -1) {
						return '<button class="btn-xs btn-warning">下架</button> ';
					} else if (row.status == 1) {
						return '<button class="btn-xs btn-info" >上架</button> ';
					}

				}
			},
			{
				title : '发表时间',
				field : 'createTime',
				align : 'center',
				width : '15%',
				formatter : function(value, row, index) {
					return Format(row.createTime, "yyyy-MM-dd hh:mm:ss");
				}
			},
			{
				title : '操作',
				field : 'headLineId',
				align : 'center',
				width : '15%',
				formatter : function(value, row, index) {
					var flag = "shangxiajia";
					//编辑
					var a = '<a  class=" btn-sm btn-info" data-toggle="modal" data-target="#modal-form" onclick="selectResourceById(' + row.headLineId + ')"><i class="fa fa-edit" ></i> 编辑</a> ';
					//删除
					var b = '<a  class=" btn-sm btn-danger"   onclick="deleteResource(' + row.headLineId + ',\'' + row.headLineTitle + '\')"><i class="fa fa-trash-o" ></i> 删除</a> ';
					//下架
					var c = '<a  class=" btn-sm btn-danger"  title="下架后前台将无法展示" onclick="updateResource(' + row.headLineId + ',\'' + row.headLineTitle + '\',-1,\'' + flag +'\')"><i class="fa fa-hand-o-down" ></i> 下架</a> ';
					//上架
					var d = '<a  class=" btn-sm btn-primary"  title="上架后将在前台展示" onclick="updateResource(' + row.headLineId + ',\'' + row.headLineTitle + '\',1,\'' + flag +'\')"><i class="fa fa-hand-o-up" ></i> 上架</a> ';
					if (row.status == -1) {
						return d + b;
					} else if (row.status == 1) {
						return a + c;
					}
					return a + b;
				}
			}
		]
	});
	globalCount++;
	returnAllCount();
}

//传参数到后台
function queryParams(params) {
	return {
		//每页多少条数据
		pageSize : params.limit,
		pageIndex : (params.offset) / params.limit + 1,
		headLineTitle : $(".search .form-control").val(),
		//resourcesContent : $(".search .form-control").val(),
		status : 0,
		typeNum : 0
	};
}


var selectResourceById = function(id) {
	var params = {
		headLineId : id
	};
	$.ajax({
		url : '/headline/getheadlinebyid',
		type : 'post',
		data : params,
		dataType : 'json',
		success : function(data) {
			var flag = "edit";
			if (data.success) {
				$("#newTitle").val(data.headLine.headLineTitle);
				$("#newLink").val(data.headLine.headLineLink);
				$("#newPriority").val(data.headLine.priority);
				$("#newType").val(data.headLine.typeNum);
				var updateButton = ' <button class="btn btn-sm btn-primary pull-right m-t-n-xs" onclick="updateResource(' + data.headLine.headLineId + ',\'' + data.headLine.headLineTitle + '\',0,\'' + flag +'\')" type="button"><strong>提交</strong></button>'
				$("#update").html(updateButton);
			} else{
				swal("查询失败", "不存在该类别信息", "error");
			}
		},
		error : function() {
			swal("查询错误", "请重新操作", "error");
		}
	});

};

//设置 字数不超过宽度限制
var operateOpinionFormatter = function(value, row, index) {
	var title = "";
	var headLineNameLow = $(".search .form-control").val().toLowerCase();
	var headLineName = $(".search .form-control").val();
	var strStartIndex = "";
	var strEndIndex = "";
	if (value.search(headLineName) != -1 || value.toLowerCase().search(headLineNameLow) != -1) {
		var strs = new Array();
		strs = value.split("");
		strStartIndex = value.indexOf(headLineName);
		strEndIndex = strStartIndex + headLineName.length - 1;
		if (value.toLowerCase().search(headLineNameLow) != -1) {
			strStartIndex = value.toLowerCase().indexOf(headLineNameLow);
			strEndIndex = strStartIndex + headLineNameLow.length - 1;
		}
		for (var i = 0; i < strs.length; i++) {
			if (i >= strStartIndex && i <= strEndIndex) {
				title += '<span style="color:#000;font-weight:bold;">' + strs[i] + '</span>';
			} else {
				title += '<span >' + strs[i] + '</span>';
			}
		}
	} else {
		title = value;
	}
	
	if (value.length > 15) {
		var num = strEndIndex - strStartIndex;
		var index = strStartIndex - 4;
		if (index < 0) {
			index = 0;
		}
		if (headLineName == "") {
			return "<span title='" + value + "'>" + title.substring(0, 150) + "..." + "</span>";
		} else {
			return "<span title='" + value + "'>" + title.substring(15 * index, (15 * index) + (num + 1) * 51 + (9 - num) * 15) + "..." + "</span>";
		}
	} else {
		return "<span  title='" + value + "'>" + title.substring(0, title.length) + "</span>";
	}
}



var formatTableUnit = function(value, row, index) {
	return {
		css : {
			"overflow" : 'hidden',
			"text-overflow" : 'ellipsis',
			"white-space" : 'nowrap'
		}
	};
};



var updateResource = function(id, title, status, flag) {
	
	if(flag == "shangxiajia"){
		var p = '将资源<span class="text-info">#' + title + '#</span>上架';
		if (status == -1) {
			p = '将资源<span class="text-info">#' + title + '#</span>下架';
		}
		var params = {
			'headLineId' : id,
			'status' : status,
			'typeNum' : 0,
			prarm : p
		};
	}
	if(flag == "edit"){
		var params = {
			'headLineId' : id,
			'headLineTitle' : $("#newTitle").val(),
			'headLineLink' : $("#newLink").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
			'priority' : $("#newPriority").val(),
			'typeNum' : $("#newType").val(),
			'status' : 0,
			prarm : '更新了资源<span class="text-info">#' + $("#newTitle").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;") + '#</span>',
		};
	}
	$.ajax({
		url : '/headline/updateheadline',
		type : 'post',
		data : params,
		dataType : 'json',
		success : function(data) {
			if (data.success) {
				initResourceCount();
				$("#modal-form").modal('hide');
				$("#allResource").bootstrapTable('refresh');
				swal("更新成功", "", "success");
			} else{
				swal("更新失败", data.msg, "error");
			}
		},
		error : function() {
			swal("更新错误", "请重新操作", "error");
		}
	});

}

var deleteResource = function(id, title) {
	var params = {
		'headLineId' : id,
		prarm : '删除的资源为<span class="text-info">#' + title + '#</span>'
	};
	swal({
		title : "确定要删除该资源吗",
		text : "删除后将无法恢复，请谨慎操作！",
		type : "warning",
		showCancelButton : true,
		confirmButtonColor : "#DD6B55",
		confirmButtonText : "删除",
		closeOnConfirm : false
	}, function() {

		$.ajax({
			url : '/headline/deleteheadlinebyid',
			type : 'post',
			data : params,
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					$("#allResource").bootstrapTable('refresh');
					initResourceCount();
					swal("删除成功！", "", "success");
				} else {
					swal("删除失败", data.msg, "error");
				}
			},
			error : function() {
				swal("删除错误", "请重新操作", "error");
			}
		});
	});


};



//图片上传  
function sendFile(file, editor, $editable) {
	var filename = false;
	try {
		filename = file['name'];
	} catch (e) {
		filename = false;
	}
	if (!filename) {
		$(".note-alarm").remove();
	}

	//以上防止在图片在编辑器内拖拽引发第二次上传导致的提示错误  
	data = new FormData();
	data.append("file", file);
	data.append("key", filename); //唯一性参数  
	$.ajax({
		data : data,
		type : "POST",
		url : "/blogadmin/uploadblogimages",
		dataType : "json",
		cache : false,
		contentType : false,
		processData : false,
		success : function(date) {
			if (date == '') {
				swal("上传失败", "请重试操作", "error");
			} else {
				var path = '图片地址  ' + date.path;
				swal("上传成功", path, "success");
			}
			editor.insertImage($editable, date.path);
		},
		error : function() {
			swal("上传错误", "请重试操作", "error");
		}
	});
}


//弹出模态框 选择图片
var selectImgPath = function(img) {
	$("#pic").modal('hide');
	var imgPath = '<img class="picPath animated fadeInRight"  style="width:100%" alt="封面" title="点击更换封面" src="' + img.src + '" />';
	$(".picPath").html(imgPath)
	$(".imagePath").val(img.src);
};

//查找服务器图库
var findPicList = function() {
	var blog = '/blog';
	$.ajax({
		//此处使用的是封装的JAVA类
		url : "/blogadmin/getFileList",
		type : "POST",
		success : function(data) {
			if (data.status == 0) {
				swal("服务器图库为空", "请上传", "error");
			} else {
				var pics = '';
				for (var i = 0; i < data.fileList.length; i++) {
					pics += '<a class="fancybox" href="javascript:void(0);"><img onclick="selectImgPath(this)" style="width: 190px; height: 115px;float:left;margin-right:3px;" alt="image" src="'  + data.fileList[i] + '"   addr="' + data.fileList[i] + '" /></a>'
				}
			}
			$(".picsList").html(pics);
		},
		error : function(e) {
			swal("获取图片错误", "请检查接口服务", "error");
		}
	});
};

var addResource = function() {
	var params = {
		headLineTitle : $("#title").val(),
		//resourcesContent : $("#content").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
		headLineLink : $("#link").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;"),
		headLineImage : $(".imagePath").val(),
		priority : $("#priority").val(),
		typeNum : $("#type").val(),
		status : 1,
		prarm : '新增的资源名称为<span class="text-info">#' + $("#title").val().replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, "&quot;").replace(/'/g, "&#039;") + '#</span>',
	};
	$.ajax({
		url : '/headline/addheadline',
		type : 'post',
		data : params,
		dataType : 'json',
		success : function(data) {
			if (data.success) {
				$("#allResource").bootstrapTable('refresh');
				initResourceCount();
				$("#title").val("");
				$("#link").val("");
				$("#priority").val("");
				$("#type").val("");
				swal("添加成功", "", "success");
			}else{
				swal("添加失败", data.msg, "success");
			}
		},
		error : function() {
			swal("添加资源错误", "请重新操作", "error");
		}
	});
};

//只有验证通过才能执行 添加
$("#addResource").click(function() {
	if ($("#commentForm").valid()) {
		addResource();
	}
});

//格式化时间
function Format(datetime, fmt) {
	if (parseInt(datetime) == datetime) {
		if (datetime.length == 10) {
			datetime = parseInt(datetime) * 1000;
		} else if (datetime.length == 13) {
			datetime = parseInt(datetime);
		}
	}
	datetime = new Date(datetime);
	var o = {
		"M+" : datetime.getMonth() + 1, //月份   
		"d+" : datetime.getDate(), //日   
		"h+" : datetime.getHours(), //小时   
		"m+" : datetime.getMinutes(), //分   
		"s+" : datetime.getSeconds(), //秒   
		"q+" : Math.floor((datetime.getMonth() + 3) / 3), //季度   
		"S" : datetime.getMilliseconds() //毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (datetime.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}